<?php 
namespace Emos\Field;

use Emos\Common;

class Text extends AbstractControl {
    protected $regexp = null;
    protected $regexpMessage = null;
    
    public function html() {
        return "<input type='text' name='{$this->htmlName}' value='".Common::plain($this->value)."' />";
    }
    
    public function validate() {            
        if(($this->regexp !== null) && !preg_match($this->regexp, $this->getPost())) {            
            $this->error = $this->regexpMessage;            
            return false;
        }
        else {
            return true;
        }
    }
    
    public function setRegexp($regexp, $regexpMessage = null) {
        if($regexpMessage === null) {
           $regexpMessage = Common::t("incorrect value"); 
        }
        
        $this->regexp = $regexp;
        $this->regexpMessage = $regexpMessage;
        
        return $this;
    }
    
    public function getFake() {        
        $fake = new \Emos\Fake();
        $sent = $fake->getSentence(1, rand(1,5));
        
        return array(
            $this->dbName => "'".$this->escapeSqlStr($sent)."'"
        );
    }     
}